/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.admin.dynamic;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.service.admin.dynamic.WorkPlanService;
import com.cos.util.AppUtil;
import com.cos.util.Const;
import com.cos.util.Jurisdiction;
import com.cos.util.ObjectExcelView;
import com.cos.util.PageData;

/**
 * TODO 工作分工    controller  
 * <p/>
 * 
 * @author <a href="15083378620@163.com">LXJ</a>
 * @version  Date: 2016年3月22日 下午1:56:28
 * @serial 1.0
 * @since 2016年3月22日 下午1:56:28
 */

@Controller
@RequestMapping(value = "/workPlan")
public class WorkPlanController extends BaseController{
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save() throws Exception{
		logBefore(logger, "新增WorkPlanController");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("WORKPLANCONTROLLER_ID", this.get32UUID());	//主键
		workPlanService.save(pd); 
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	/*
	 *  菜单地址url(权限用)
	 */
	String menuUrl = "workPlan/list.do";
	@Resource(name="workPlanService")
	private WorkPlanService workPlanService;
	
	@RequestMapping(value = "/list")
	public ModelAndView list(Page page){
		System.out.println("---------------------workplan-------");
		logBefore(logger,"列表workPlan");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData> varList = workPlanService.list(page);
			mv.setViewName("admin/dynamic/workPlan_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		}catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC() {
		Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
	}
	
	
	@RequestMapping(value = "delete")
	public  void delete(PrintWriter out){
		System.out.println("---------------------workplandelete-------");
		logBefore(logger,"删除用户动态标签");
		PageData pd = new PageData();
		try {
			if(Jurisdiction.buttonJurisdiction(menuUrl,"del")){
				pd = this.getPageData();
				//System.out.println(pd.get(key));
				workPlanService.delete(pd);
			}
			out.write("success");
			out.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		logBefore(logger, "修改WorkPlanController");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		workPlanService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 去新增页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd() throws UnsupportedEncodingException{
		logBefore(logger, "去新增WorkPlanController页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("admin/dynamic/workPlan_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	/**
	 * 去修改页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit() throws UnsupportedEncodingException{
		logBefore(logger, "去修改WorkPlanController页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = workPlanService.findById(pd);	//根据ID读取
			mv.setViewName("admin/dynamic/workPlan__edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除WorkPlanController");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "dell")){return null;} //校验权限
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if(null != DATA_IDS && !"".equals(DATA_IDS)){
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				workPlanService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel() throws UnsupportedEncodingException{
		logBefore(logger, "导出WorkPlanController到excel");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;}
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("CONTENT");	//1
			titles.add("CREATE_TIME");	//2
			titles.add("U_ID");	//3
			titles.add("C_ID");	//4
			titles.add("D_ID");	//5
			titles.add("END_TIME");	//6
			titles.add("START_TIME");	//7
			titles.add("MONEY");	//8
			titles.add("STAND");	//9
			titles.add("SUPPORT_NUM");	//10
			titles.add("TRAMPLE_NUM");	//11
			titles.add("TAG_ID");	//12
			dataMap.put("titles", titles);
			List<PageData> varOList = workPlanService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("CONTENT"));	//1
				vpd.put("var2", varOList.get(i).getString("CREATE_TIME"));	//2
				vpd.put("var3", varOList.get(i).getString("U_ID"));	//3
				vpd.put("var4", varOList.get(i).getString("C_ID"));	//4
				vpd.put("var5", varOList.get(i).getString("D_ID"));	//5
				vpd.put("var6", varOList.get(i).getString("END_TIME"));	//6
				vpd.put("var7", varOList.get(i).getString("START_TIME"));	//7
				vpd.put("var8", varOList.get(i).getString("MONEY"));	//8
				vpd.put("var9", varOList.get(i).getString("STAND"));	//9
				vpd.put("var10", varOList.get(i).get("SUPPORT_NUM").toString());	//10
				vpd.put("var11", varOList.get(i).get("TRAMPLE_NUM").toString());	//11
				vpd.put("var12", varOList.get(i).get("TAG_ID").toString());	//12
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
